package com.fbli.service.Impl;

import com.fbli.mapper.ProductsMapper;
import com.fbli.pojo.Products;
import com.fbli.service.ProductsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
@Service
public class ProductsServiceImpl implements ProductsService {
    @Autowired
    private ProductsMapper productsMapper;
    @Override
    public List<Products> selectAll() {
        return productsMapper.selectAll();
    }

    @Override
    public PageInfo<Products> selectPage(int pageNum, int pageSize) {
        //分页开始
        PageHelper.startPage(pageNum, pageSize);
        List<Products> products = productsMapper.selectAll();
        //返回分页后的结果
        PageInfo<Products> productsPageInfo = new PageInfo<>(products);
        return productsPageInfo;
    }

    @Override
    public List selectCategory() {
        return productsMapper.selectCategory();
    }

    @Override
    public List selectBrand() {
        return productsMapper.selectBrand();
    }

    @Override
    public List selectFunction() {
        return productsMapper.selectFunction();
    }

    @Override
    public PageInfo<Products> selectCondition(Products products,int pageNum, int pageSize) {
        //分页开始
        PageHelper.startPage(pageNum, pageSize);
        List<Products> products1 = productsMapper.selectCondition(products);
        //返回分页后的结果
        PageInfo<Products> productsPageInfo = new PageInfo<>(products1);
        productsPageInfo.toString();
        return productsPageInfo;
    }

    @Override
    public void deleteById(long[] ids) {
        for (long productId:ids){
            productsMapper.deleteByPrimaryKey((int) productId);
        }

    }

    @Override
    public void update(Products products) {


        Timestamp time = new Timestamp(new Date().getTime());
        products.setReleaseTime(time);
        productsMapper.updateByPrimaryKey(products);

    }

    @Override
    public Products selectById(int productId) {
        return productsMapper.selectByPrimaryKey(productId);
    }

    @Override
    public void insert(Products products) {
        Timestamp time = new Timestamp(new Date().getTime());
        products.setReleaseTime(time);
        productsMapper.insert(products);
    }
}
